package server

import (
	"context"
	"encoding/json"
	"errors"
	"ke-srv/basic/config"
	l "ke-srv/basic/init"
	__ "ke-srv/basic/proto"
	"ke-srv/handler/models"
	"time"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedUserServer
}

// 用户注册
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	var user models.User
	if err := user.GetUserInfo(in.Mobile); err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID != 0 {
		return nil, errors.New("用户已存在")
	}
	user = models.User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	if err := user.RegisterUser(); err != nil {
		return nil, errors.New("用户注册失败")
	}
	return &__.RegisterResp{
		UserId: int64(user.ID),
	}, nil
}

// 用户登录
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user models.User
	if err := user.GetUserInfo(in.Mobile); err != nil {
		return nil, errors.New("获取用户信息失败")
	}
	if user.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	if user.Password != in.Password {
		return nil, errors.New("密码错误")
	}
	return &__.LoginResp{
		UserId: int64(user.ID),
	}, nil
}

// 课程展示
func (s *Server) ListKe(_ context.Context, in *__.ListKeReq) (*__.ListKeResp, error) {
	var ke models.Ke
	list, _ := ke.ListKe()
	var listKe []*__.ListKe
	get := config.Rdb.Get(l.Ctx, "listKe").Val()
	if get == "" {
		for _, v := range list {
			listKe = append(listKe, &__.ListKe{
				KeId:    int64(v.ID),
				UserId:  int64(v.UserId),
				Title:   v.Title,
				Comment: v.Comment,
				Num:     int64(v.Num),
				Price:   float32(v.Price),
				Address: v.Address,
			})
		}
		marshal, _ := json.Marshal(&listKe)
		config.Rdb.Set(l.Ctx, "listKe", marshal, time.Minute*3)
	} else {
		_ = json.Unmarshal([]byte(get), &listKe)
	}

	return &__.ListKeResp{
		List: listKe,
	}, nil
}

// 课程删除
func (s *Server) DeleteKe(_ context.Context, in *__.DeleteKeReq) (*__.DeleteKeResp, error) {
	var ke models.Ke
	if err := ke.GetKeById(int(in.KeId)); err != nil {
		return nil, errors.New("查看课程失败")
	}
	if ke.ID == 0 {
		return nil, errors.New("课程不存在")
	}
	ke.ID = uint(in.KeId)
	if err := ke.DeleteKe(); err != nil {
		return nil, errors.New("删除课程失败")
	}
	return &__.DeleteKeResp{
		KeId: int64(ke.ID),
	}, nil
}

// 课程分页
func (s *Server) KeList(_ context.Context, in *__.KeListReq) (*__.KeListResp, error) {
	var ke models.Ke
	list, _ := ke.KeList(int(in.Page), int(in.PageSize), in.Title)
	var listKe []*__.KeList
	for _, v := range list {
		listKe = append(listKe, &__.KeList{
			KeId:    int64(v.ID),
			UserId:  int64(v.UserId),
			Title:   v.Title,
			Comment: v.Comment,
			Num:     int64(v.Num),
			Price:   float32(v.Price),
			Address: v.Address,
		})
	}

	return &__.KeListResp{
		List: listKe,
	}, nil
}

// 课程详情
func (s *Server) DetailKe(_ context.Context, in *__.DetailKeReq) (*__.DetailKeResp, error) {
	var ke models.Ke
	ke.ID = uint(in.KeId)
	if err := ke.DetailKe(int(in.KeId)); err != nil {
		return nil, errors.New("详情展示失败")
	}
	return &__.DetailKeResp{
		Title:   ke.Title,
		Price:   float32(ke.Price),
		Address: ke.Address,
		Img:     ke.Img,
		Num:     int64(ke.Num),
	}, nil
}

// 用户收藏表
func (s *Server) CollectUser(_ context.Context, in *__.CollectUserReq) (*__.CollectUserResp, error) {
	var ke models.Ke
	if err := ke.CollectUser(int(in.Id)); err != nil {
		return nil, errors.New("详情展示失败")
	}
	return &__.CollectUserResp{
		KeId:    int64(ke.ID),
		UserId:  int64(ke.UserId),
		Title:   ke.Title,
		Comment: ke.Comment,
		Num:     int64(ke.Num),
		Price:   float32(ke.Price),
		Address: ke.Address,
		Img:     ke.Img,
	}, nil
}
